/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _GD25Q_HAL_MODEL_
#include "gd32f10x.h"
#include "gd25q_hal.h"
#include "gd25q_cmd.h"


#define GDSPI                                     SPI1
#define ISR_IRQn                                  SPI1_IRQn

#define PDMA_CH_SPI_RX                            11
#define PDMA_CH_SPI_TX                            10

static uint32_t DataFixed;

static void gdmaRxIsr(void);
static void gdmaTxIsr(void);

void FlashQ25ReadDmaCallBack(void);

/*****************************************************************************//*!
* @brief   hal init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/    
void gdSpiInit()
{
    spi_parameter_struct spi_init_struct;
    
    //cs
    gpio_init(GPIOE, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
    GD25Cs(1);
    
    rcu_periph_clock_enable(RCU_SPI0);
    rcu_periph_reset_enable(RCU_SPI0RST);
    rcu_periph_reset_disable(RCU_SPI0RST);
    
    /* SPI0_SCK(PA5), SPI0_MISO(PA6) and SPI0_MOSI(PA7) GPIO pin configuration */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    
    
    /* SPI0 parameter config */
    spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
    spi_init_struct.device_mode          = SPI_MASTER;;
    spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;;
    spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
    spi_init_struct.nss                  = SPI_NSS_SOFT;
    spi_init_struct.prescale             = SPI_PSC_8 ;
    spi_init_struct.endian               = SPI_ENDIAN_MSB;;
    spi_init(SPI0, &spi_init_struct);

    /* set crc polynomial */
    //spi_crc_polynomial_set(SPI0,7);
    /* enable SPI0 */
    spi_enable(SPI0);
    
    b25qDmaBusy =0;                           
}

/*****************************************************************************//*!
* @brief   spi data read and write.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/ 
uint8_t dgSpiRw(uint8_t dataW)
{

    volatile uint8_t dataR;
    
    while(SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);
    SPI_DATA(SPI0) = (uint32_t)dataW;
    NOP8();
    while(!(SPI_STAT(SPI0) & SPI_STAT_RBNE));
    dataR =  SPI_DATA(SPI0);
    return ( dataR );
}

uint8_t dgSpiRead(uint8_t *datar, uint32_t Len)
{

    volatile register  uint8_t dataR;

    while(SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);
    while(Len--)
    {
        SPI_DATA(SPI0) = 0xFF;
        NOP8();
        while(!(SPI_STAT(SPI0) & SPI_STAT_RBNE));
        *datar++ =  SPI_DATA(SPI0);
    }
    return ( dataR );
}

uint8_t dgSpiWrite(uint8_t *dataw, uint32_t Len)
{
    volatile register uint8_t dataR;
    
    while(SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);

    while(Len--)
    {
        SPI_DATA(SPI0) = *dataw++;
        NOP8();
        while(!(SPI_STAT(SPI0) & SPI_STAT_RBNE));
        dataR =  SPI_DATA(SPI0);
    }

    return ( dataR );
}

/*****************************************************************************//*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/ 
void gdSpiDmaRead(uint8_t *Data, uint32_t  Len)
{                            
    b25qDmaBusy =1;
}

/*****************************************************************************//*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/ 
void gdSpiDmaWrite(uint8_t *Data, uint32_t  Len){

    b25qDmaBusy =1;
}

/*****************************************************************************//*!
* @brief   RX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void gdmaRxIsr(){
  
    b25qDmaBusy = 0;

}

/*****************************************************************************//*!
* @brief   TX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void gdmaTxIsr(){
   
}
